// ==UserScript==
// @name Wrapup Access
// @description Applies a previously constructed wrapper, if found, to rewrite datasets on the page as more accessible tables.
// @require http://code.jquery.com/jquery-2.1.1.min.js
// @require https://raw.githubusercontent.com/steven-h-gardiner/jquery-xpath/master/jquery.xpath.js
// @require ./content/js/smartwrap-docmarker.js
// @require ./content/js/wrapup.js
// @require ./content/js/wrapup-tablify.js
// @resource wrapped ./skin/wrapped.css
// @resource access_template ./content/ui/accessTable_template.html
// @author Steve Gardiner
// @include *
// @grant GM_getResourceText
// @grant GM_addStyle
// @grant unsafeWindow
// ==/UserScript==

// this should not be active anymore
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @require https://wrapup-client.googlecode.com/hg/content/js/wrapup.js

(function(jQuery) {

  console.log("PEEKABOO");

  jQuery(document).on('ready', function() {
    jQuery(document).on('keydown', function(evt) {
      if ((evt.key && (evt.key === 'Insert')) || (evt.which === 45)) {
	jQuery(document).data('insertpressed', true);
      }
    });
    jQuery(document).on('keyup', function(evt) {
      if ((evt.key && (evt.key === 'Insert')) || (evt.which === 45)) {
	jQuery(document).removeData('insertpressed');
      }
    });
    jQuery(document).on('keydown', function(evt) {
      if ((evt.which <= 40) && (evt.which >= 33)) {
	if (evt.ctrlKey && evt.altKey) {
	  jQuery(document).trigger('wrapup_feedback', [{message:'tablecmd',key:(evt.key || evt.which)}]);
	}
	if (jQuery(document).data('insertpressed')) {
	  jQuery(document).trigger('wrapup_feedback', [{message:'tablecmd',key:(evt.key || evt.which)}]);
	}
      }
    });
  });
  
  jQuery(document).on('wrapup-applied-wrapper', function() {
    tablify.setTemplate(jQuery('.wrapup-table'));
    var tables = tablify.tablify();

    for (itemscope in tables) {
      console.log("TABLE: " + itemscope);
      var table = tables[itemscope];
      try {
	jQuery(table.table).insertBefore(jQuery(table.container));
	jQuery(table.container).hide();
      } catch (ee) {
	console.error("exception: " + ee.message);
      }
    }

    jQuery("table.wrapup-table").attr("border", "1");
    jQuery("table.wrapup-table td > td").replaceWith(function() { return jQuery("<span/>").append(jQuery(this).contents()); });
    jQuery("table:not(.wrapup-table) > tbody > tr > td").replaceWith(function() { return jQuery("<span/>").append(jQuery(this).contents()); });
    jQuery("table:not(.wrapup-table) > tbody > tr").replaceWith(function() { return jQuery("<div/>").append(jQuery(this).contents()); });
    jQuery("table:not(.wrapup-table)").replaceWith(function() { return jQuery("<div/>").append(jQuery(this).contents()); });

  });
}(window.jQuery));
